package org.cug.service.impl;

import org.cug.mapper.CategoryMapper;
import org.cug.pojo.Category;
import org.cug.service.CategoryService;
import org.cug.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * @Author: gongshihao
 * @Date: 2024-05-15-11:05
 * @Description: this is my code
 */
@Service
public class CategoryServiceimpl  implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;
    @Override
    public void add(Category category) {
        //补充属性值
        category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer userid = (Integer) map.get("id");
        category.setCreateUser(userid);//创建人的id
        categoryMapper.add(category);
    }

    @Override
    public List<Category> lists() {
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer userid = (Integer)map.get("id");
       return  categoryMapper.lists(userid);
    }

    @Override
    public Category findById(Integer id) {
        Category c=categoryMapper.findById(id);
        return c;
    }

    @Override
    public void update(Category category) {
        category.setUpdateTime(LocalDateTime.now());
        categoryMapper.update(category);
    }
}
